package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CollectedExhibitionEntity;
import com.zw.yzk.kabu.data.network.task.CollectedExhibitionTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.model.CollectedExhibitionMapper;
import com.zw.yzk.kabu.view.view.CollectedExhibitionView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/10.
 */

public class CollectedExhibitionPresenter implements Presenter {

    private UseCase getCollectedList;
    private AppCompatActivity activity;
    private CollectedExhibitionMapper mapper;
    private CollectedExhibitionView exhibitionView;

    @Inject
    public CollectedExhibitionPresenter(@Named("collectedExhibitionUseCase")UseCase getCollectedList,
                                        AppCompatActivity activity,
                                        CollectedExhibitionMapper mapper) {
        this.getCollectedList = getCollectedList;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setExhibitionView(CollectedExhibitionView view) {
        exhibitionView = view;
    }

    public void getCollectedExhibitionList(int start) {
        getCollectedList.execute(new CollectedExhibitionSubscriber(activity), new CollectedExhibitionTask(start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getCollectedList.unSubscribe();
    }

    private class CollectedExhibitionSubscriber extends BaseSubscriber<CollectedExhibitionEntity> {

        public CollectedExhibitionSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(CollectedExhibitionEntity collectedExhibitionEntity) {
            super.onNext(collectedExhibitionEntity);
            exhibitionView.getCollectedExhibitionListSuccess(mapper.transfer(collectedExhibitionEntity.result.exhibitionList));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            exhibitionView.getCollectedExhibitionListFailed();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_collected_exhibition_list_failed);
        }
    }
}
